﻿using SimpleOrmCFPC.Apis.Db;
using SimpleOrmCFPC.Core.RequestsComputer;
using SimpleOrmCFPC.Core.RequestsComputer.Dialects.Sqlite;
using SimpleOrmCFPC.engines.sqlite;
using SimpleOrmCFPC.upgrade;

namespace SimpleOrmCFPC.Core.Orm
{
    public class RealOrm : IOrm
    {
        public interface IDaoFactory
        {
            IDao<T> Create<T>() where T : new();
        }

        private IDaoFactory daoFactory;
        private IDb db;
        private readonly ISchemaProvider schemaProvider;

        public RealOrm(IDaoFactory daoFactory, IDb db, ISchemaProvider schemaProvider)
        {
            this.daoFactory = daoFactory;
            this.db = db;
            this.schemaProvider = schemaProvider;
        }

        public IDao<T> CreateDao<T>()
            where T : new()
        {
            return daoFactory.Create<T>();
        }

        public void Register<T>()
        {
            Upgrader upgrader = new Upgrader(db, schemaProvider, new CreateTableRequestComputer(new SQLiteTableDialect(new DbToSqlTypeConverter()), new SchemaFromTypeExtractor()));
            upgrader.Table<T>();
        }
    }
}